//
//  LoginControler.m
//  WoChuangFuHD
//
//  Created by 陈贵邦 on 15/10/15.
//  Copyright © 2015年 陈贵邦. All rights reserved.
//

#import "LoginControler.h"
#import "LoginView.h"
#import "RealnameVC.h"
@interface LoginControler ()<HttpBackDelegate,LoginViewDelegate>{
    LoginView *loginView;
}

@end

@implementation LoginControler

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if([AppDelegate shareMyApplication].isAutoLogin)[self sendLoginAppRequest];
}

- (void)viewDidLoad {
    [self setNavigationBar];
    [super viewDidLoad];
    [self loadViewLoginView];
    // Do any additional setup after loading the view.
}

-(void)setNavigationBar{
    self.navigationItem.title = @"沃创富";
    [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
    [self.navigationController.navigationBar  setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
//    backItem.title = @"退出";
//    self.navigationItem.backBarButtonItem = backItem;
//    self.navigationController.delegate = self;
}

-(void)loadViewLoginView{
    loginView = [[LoginView alloc]initWithFrame:self.view.bounds];
    loginView.delegate = self;
    [self.view addSubview:loginView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)sendLoginAppRequest{
    
        bussineDataService *buss=[bussineDataService sharedDataServicee];
        buss.target=self;
        buss.HUDStatusString = @"登录中";
        NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:
                           loginView.PassTextField.text,@"passWd",
                          loginView.NameTextField.text,@"userCode",
                           nil];
        [buss qiankaLogin:dic];
 
    
}

-(void)requestDidFinished:(NSDictionary *)info{
    NSString* oKCode = @"0000";
    NSString* bizCode = [info objectForKey:@"bussineCode"];
    NSString* errCode = [info objectForKey:@"errorCode"];
    if([[QianKaLoginMessage getBizCode] isEqualToString:bizCode]){
        if([oKCode isEqualToString:errCode]){
 
            bussineDataService *bus=[bussineDataService sharedDataServicee];
            
            NSMutableString *nameStr= [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
            NSLog(@"bus,rspInfo是%@",bus.rspInfo);
            NSString *userId = bus.rspInfo[@"userId"];
            
            MyLog(@"\n\n\n\n\nuserId id %@\n\n\n\n\n",userId);
            NSString *userCode = bus.rspInfo[@"userCode"];
            NSString *qq = bus.rspInfo[@"qq"];
            NSString *sessionid = bus.rspInfo[@"sessionId"];;
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:sessionid forKey:@"sessionid"];
            [user setObject:qq forKey:@"qq"];
            [[NSUserDefaults standardUserDefaults] setObject:userId forKey:@"userId"];
            [[NSUserDefaults standardUserDefaults] setObject:userCode forKey:@"userCode"];
            //            [[NSUserDefaults standardUserDefaults] setObject:userType forKey:@"userType"];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            
            [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"isAutoLogin"];
            
            
            RealnameVC *realnameVC = [[RealnameVC alloc]init];
            [self.navigationController pushViewController:realnameVC animated:YES];

    }
    }
    
    
}
-(void)requestFailed:(NSDictionary *)info{
    
    NSString *msg = [info objectForKey:@"MSG"];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}


-(void)didClickedLogin{
    [self sendLoginAppRequest];
}

@end
